1008B - Turn the Rectangles - CodeForces Solution


greedy sortings *1000

Please click on ads to support us..

Python Code:

def solve():
    n=int(input())     x=10**9     for _ in range(n):         w,h=sorted(map(int,input().split()))         if h<=x:             x=h            elif w<=x:             x=w            else:
            print('NO')             return
    print('YES')

solve()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define int long long

bool isPrime(int n)
{
  for (int i = 2; i * i <= n; ++i)
  {
    if (n % i == 0)
    {
      return false;
    }
  }
  return true;
}
int mod = 1e9 + 7;
int power(int x, int y)
{
  int z = 1;

  x = x % mod;
  while (y > 0)
  {
    if (y & 1)
      z = (z * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return z;
}
int lcm(int a, int b)
{
  return (a * b) / __gcd(a, b);
}

bool so(const pair<int, int> &a,
        const pair<int, int> &b)
{
  if (a.first != b.first)
    return (a.first < b.first);
  else
    return (a.second > b.second);
}

int getsum(int n)
{
  int sum = 0;
  while (n != 0)
  {
    sum = sum + n % 10;
    n = n / 10;
  }
  return sum;
}

void sort(map<int, int> &M) // sort map by value
{

  // Declare a multimap
  multimap<int, int> MM;

  // Insert every (key-value) pairs from
  // map M to multimap MM as (value-key)
  // pairs
  for (auto &it : M)
  {
    MM.insert({it.second, it.first});
  }
}

signed main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // int t;
  // cin >> t;
  // while (t--)
  // {
     int n;
     cin>>n;
     vector<int> v1;
     vector<int> v2;
     for(int i=0;i<n;i++){
      int w,h;
      cin>>w>>h;
      v1.push_back(max(w,h));
      v2.push_back(min(w,h));
     }
     int prev=v1[0];
     bool f=true;
     for(int i=0;i<n-1;i++){
      if(prev<v2[i+1]){
        f=false;
        break;
      }
      else{
        if(prev>=v1[i+1]){
          prev=v1[i+1];
        }
        else{
          prev=v2[i+1];
        }
      }
     }
     if(f){
      cout<<"YES"<<endl;
     }
     else{
      cout<<"NO"<<endl;
     }


  // }
}


Comments

Submit
0 Comments
More Questions

714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe
1674A - Number Transformation
1244E - Minimizing Difference
1688A - Cirno's Perfect Bitmasks Classroom
219A - k-String
952A - Quirky Quantifiers
451B - Sort the Array
1505H - L BREAK into program
171E - MYSTERIOUS LANGUAGE
630D - Hexagons
1690D - Black and White Stripe
1688D - The Enchanted Forest
1674C - Infinite Replacement
712A - Memory and Crow
1676C - Most Similar Words